package com.example.demo.util;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

public class OAuth2Servlet extends HttpServlet {

    private static String APPID = "wxfxxxxxxxxxxxxx";//这个是你服务号appid，和URL里面的appid是一个意思
    private static String APPSECRET = "xxxxxxxxxxxxxxxxxxxxx"; //这个是你服务号的app秘钥
    private static String ACCESS_URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=APPSECRET&code=CODE&grant_type=authorization_code"; //这个是请求获取用户信息的URL

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
//        response.setCharacterEncoding("UTF-8");
        String code = request.getParameter("code");//获取OAuth2.0请求后，服务器返回的code内容,这个code在接下来向微信服务请求用户信息的时候要用到
        String requestUrlString = ACCESS_URL.replace("APPID", APPID).replace("APPSECRET", APPSECRET).replace("CODE", code);//将请求用户的URL中的///参数替换成真正的内容
        URL url = new URL(requestUrlString);  //创建url连接
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); //打开连接
        urlConnection.setDoOutput(true);
        urlConnection.setDoInput(true);
        urlConnection.setRequestMethod("GET");
        urlConnection.setUseCaches(false);
        urlConnection.connect();
        BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "utf-8"));
        StringBuffer buffer = new StringBuffer();
        //<span style="font-family: Arial, Helvetica, sans-serif;">//存储服务器返回的信息</span>
        String line = "";
        String openid = "";   //用来接收用户的appid
        while ((line = reader.readLine()) != null) {
            buffer.append(line);
        }
        String result = buffer.toString();
        System.out.println(result);
        try {
            JSONObject resultObject = new JSONObject(result); //将服务器返回的字符串转换成json格式
            openid = resultObject.getString("openid");  //获取得到用户appid
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        request.setAttribute("code", code);
        request.setAttribute("openid", openid);
        RequestDispatcher res = request.getRequestDispatcher("../weixin_index.jsp");  //跳转页面
        res.forward(request, response);
    }



}
